/*
 License
 
 Copyright © 2012 Kevin Kimmich
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights to 
 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 the Software, and to permit persons to whom the Software is furnished to do so, 
 subject to the following conditions:
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "crcore.h"

//Map a file with "filename" into memory. If no_swap is not zero,
//read the entire file into RAM and do not allow the kernel to swap it
//out.

void* map_file( mapped_file_t* mf, const char* filename, int no_swap )
{
	memset(mf,0,sizeof(mapped_file_t));
	mf->fd = open(filename,O_RDONLY);
	if(mf->fd<0)
		return NULL;
	
	int err = fstat( mf->fd , &mf->st );
	if(err < 0)
	{
		close(mf->fd);
		mf->fd=0;
		return NULL;
	}
	
	//map the entire file into process address space as a private mapping
	mf->data = (char*) mmap(NULL,mf->st.st_size,PROT_READ,MAP_PRIVATE,mf->fd,0);
	if(mf->data == MAP_FAILED)
	{
		close(mf->fd);
		mf->fd = 0;
		mf->data = NULL;
		return NULL;
	}
	
	//if no_swap is set, lock the mapped file into RAM.
	//the kernel will read the file pages into RAM and not
	//swap them out. Note, if this operation fails, it should 
	//be logged, but will only affect performance, not
	//prevent operation.
	mf->no_swap = no_swap;
	if(mf->no_swap)
		mlock(mf->data,mf->st.st_size);
	
	mf->filename = strdup(filename);
	
	return mf->data;
}

//unmap the file, and free resources
void unmap_file( mapped_file_t* mf )
{
	if(mf->filename)
		free(mf->filename);
	
	if(mf->no_swap)
		munlock(mf->data,mf->st.st_size);
	
	if(mf->data)
		munmap(mf->data, mf->st.st_size);
	
	if(mf->fd)
		close(mf->fd);
	
	memset(mf,0,sizeof(mapped_file_t));
}

int iterate_files(const char* path, int* count, void* obj, iterate_file_callback callback)
{
	char full_path[LOCAL_BUF_SIZE];
	char* cwd = getcwd(NULL,0);
	if(cwd == NULL)
		return -1;
	
	if(chdir(path))
	{
		chdir(cwd);
		free(cwd);
		return -1;
	}
	
	char* fwd = getcwd(NULL,0);
	
	DIR* dir = opendir(".");
	if(dir)
	{
		struct dirent* d = NULL;
		do
		{
			d = readdir(dir);
			if(d)
			{
				struct stat stat;
				lstat(d->d_name,&stat);
				if(S_ISDIR(stat.st_mode))
				{
					if(strcmp(d->d_name,".")==0 || strcmp(d->d_name,"..")==0)
					{
					}
					else
					{
						snprintf(full_path,LOCAL_BUF_SIZE,"%s/%s/",fwd,d->d_name);
						callback(obj,count, 0,1,full_path,d->d_name,&stat);
					}
				}
				else
				{
					if(S_ISREG(stat.st_mode))
					{
						snprintf(full_path,LOCAL_BUF_SIZE,"%s/%s",fwd,d->d_name);
						callback(obj,count,1,0,full_path,d->d_name,&stat);
					}
					else
					{
						if(S_ISLNK(stat.st_mode))
						{
						}
					}
				}
			}
		}while(d);
		closedir(dir);
	}
	
	chdir(cwd);
	free(cwd);
	free(fwd);
	return 0;
}

